<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    title="报警设置模板选择"
    style="min-width: 1200px"
    draggable
    @close="closeDialog"
  >
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="设备类型">
          <el-select
            style="width: 100%"
            v-model="searchParam.relationId"
            placeholder="请选择设备类型"
            clearable
          >
            <el-option
              v-for="item in equipTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警等级">
          <el-select
            v-model="searchParam.alarmLevel"
            placeholder="请选择报警等级"
            clearable
          >
            <el-option
              v-for="item in useDictionary('ScaAlarmLevel')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="报警名称">
          <el-input
            v-model="searchParam.alarmName"
            placeholder="请输入报警名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        @current-change="handleCurrentChange1"
      >
        <el-table-column
          label="报警编号"
          prop="alarmCode"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="报警名称"
          prop="alarmName"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="报警等级" prop="alarmLevel" min-width="100">
          <template #default="scope">
            {{ useDictionary("ScaAlarmLevel", scope.row.alarmLevel) }}
          </template>
        </el-table-column>
        <el-table-column
          label="持续时间(秒)"
          prop="sustainedTime"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="时间间隔(秒)"
          prop="intervalTime"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="报警内容"
          prop="alarmContent"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="copySubmit()"
          >确 定
        </el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { useTable, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRouter, useRoute } from "vue-router";
defineOptions({
  name: "ADMINSCAALARMSETTING",
  inheritAttrs: false,
});

const emit = defineEmits(["change-visible1"]);
const route = useRoute();
const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

// 请求参数
const requestApi = "/iotadmin/scaalarmsetting/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  alarmCode: "",
  alarmName: "",
  alarmLevel: "",
  relationId: "",
};

const deleteParam = {
  api: "/iotadmin/scaalarmsetting/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

onMounted(() => {
  getTableList();
});

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible1", false);
}

// 获取设备类型下拉框
const equipTypeList: any = ref([]);
const getEquipTypeList = async () => {
  const res = await request({
    url: "/iotadmin/scaequiptype/getlist",
    method: "post",
    data: {},
  });
  if (res.code === 200) {
    equipTypeList.value = res.data;
    equipTypeList.value?.map((item) => {
      item.label = `${item.equipTypeCode}-${item.equipTypeName}`;
      item.value = `${item.id}`;
    });
  }
};
// 选择高亮
const currentRow = ref();
const handleCurrentChange1 = (val: any) => {
  currentRow.value = val;
};
// 复制提交
const copySubmit = async () => {
  console.log(currentRow.value.id);

  if (currentRow.value) {
    const res = await request({
      url: "/iotadmin/scaequipinfo/copyAlarm",
      method: "post",
      data: { alarmId: currentRow.value.id, id: route.query.id },
    });
    emit("change-visible1", false);
  } else {
    return ElMessage.warning("请选择复制模板");
  }
};

onMounted(() => {
  getEquipTypeList();
});
</script>
